
import java.net.*; 
import java.io.*; 

import common.Shared;

public class FTP_Server implements Runnable
{
	private static final int 
			DEFAULT_PORT = 8264;
	private int listenPort;
	
	/** F T P _ S e r v e r (   )
	 * ------------------------------------------------------------------------
	 *   Creates a new FTP_Handler thread for every client trying to connect.
	 */
	public FTP_Server 
			( int listenPort )
	{
		this.listenPort = listenPort;
	}

	@Override
	public void run() {
		ServerSocket server = null;
		int port = ( listenPort <= 1024 ? DEFAULT_PORT : listenPort );

		try {
			server = new ServerSocket
					( port );
		}
		catch( IOException e )
		{
			System.out.println
					( " ** FATAL ERROR: Could not create server socket! " +
					  "Aborting. **" );
			System.exit( 1 );
		} 
		while( true )
		{
			try {
				( new FTP_Handler( server.accept() ) ).start();
			} catch( IOException e ) {
				Shared.error( "Could not establish connection with host!" );
			}
		}
	}

}
